package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.enterprise.SecerTitleEntity;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServiceSearchModel extends AndroidViewModel {
    private List<MenuEntity> listdata = new ArrayList<>();
    public MutableLiveData<List<MenuEntity>> secondLevelData = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public List<String> checkData = new ArrayList<>();
    public String mitemid;
    public MutableLiveData<Integer> isgz = new MutableLiveData<>();
    public MutableLiveData<String> dataid = new MutableLiveData<>();

    public String companyId = "";

    public ServiceSearchModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void setMitemid(String id) {
        this.mitemid = id;
    }


    /**
     * 二级分类
     */

    public void getServerTitleLable() {
        ShowLoading.postValue(1);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getserviceTitleLable().enqueue(new Callback<BaseEntity<List<SecerTitleEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<SecerTitleEntity>>> call, Response<BaseEntity<List<SecerTitleEntity>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    listdata.clear();
                    for (SecerTitleEntity mdata : response.body().getData()) {
                        MenuEntity menuEntity = new MenuEntity();
                        if (!TextUtils.isEmpty(mdata.getCategoryThirdName())) {
                            menuEntity.setName(mdata.getCategoryThirdName());
                        } else if (!TextUtils.isEmpty(mdata.getCategorySecondName())) {
                            menuEntity.setName(mdata.getCategorySecondName());
                        } else {
                            menuEntity.setName(mdata.getCategoryFirstName());
                        }

                        //字符太长
                        if (menuEntity.getName().length() > 6) {
                            String name = menuEntity.getName().substring(0, 5) + "...";
                            menuEntity.setName(name);
                        } else {
                            menuEntity.setName(menuEntity.getName());
                        }
                        menuEntity.setAppIcon(mdata.getImgUrl());
                        listdata.add(menuEntity);
                    }
                    secondLevelData.postValue(listdata);
                } else {
                    secondLevelData.postValue(null);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<SecerTitleEntity>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    public void setCheckData(){
        checkData.add("公司注册");
        checkData.add("代理记账");
        checkData.add("资质办理");
        checkData.add("税务筹划");
        checkData.add("法律服务");
        checkData.add("知产交易");
    }
}


